package com.tangr.adapterapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.tangr.normaladapter.recyclerview.MultiItemAdapter;
import com.tangr.normaladapter.recyclerview.MultiItemTypeSupport;
import com.tangr.normaladapter.recyclerview.OnItemClickListener;
import com.tangr.normaladapter.ViewHolder;
import com.tangr.normaladapter.recyclerview.StaggerGridAdapter;

import java.util.ArrayList;
import java.util.List;

public class RecyclerViewActivity extends AppCompatActivity {
    private RecyclerView mRecyclerView;
    private List<String> mDatas = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        initDatas();

        mRecyclerView = (RecyclerView) findViewById(R.id.id_recyclerview);

        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
//        mRecyclerView.setLayoutManager(new GridLayoutManager(this,3,GridLayoutManager.VERTICAL,false));
//        mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL));
//        mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
//        final StaggerGridAdapter<String> adapter = new StaggerGridAdapter<String>(this, mDatas, R.layout.list_item) {
//            @Override
//            public void setView(ViewHolder holder, String item) {
//                holder.setText(R.id.item_tv, item);
//                holder.setImage(R.id.item_iv, R.mipmap.ic_launcher);
//            }
//        };
        final MultiItemAdapter<String> adapter = new MultiItemAdapter<String>(this, mDatas, new MultiItemTypeSupport<String>() {
            @Override
            public int getLayoutId(int type) {
                if (type==0)
                    return R.layout.list_item;
                return R.layout.list_item1;
            }

            @Override
            public int getItemViewType(int position, String s) {
                if (position%2==0)
                    return 0;
                return 1;
            }

            @Override
            public boolean isEnabled(int viewType){
                if(viewType==1)
                    return false;
                return true;
            }
        }) {
            @Override
            public void setView(ViewHolder holder, String item) {
                if(holder.getLayoutId()==R.layout.list_item){
                    holder.setText(R.id.item_tv,item);
                    holder.setImage(R.id.item_iv, R.mipmap.ic_launcher);
                }else {
                    holder.setText(R.id.item_tv,item);
                    holder.setImage(R.id.item_iv, R.mipmap.ic_launcher);
                }
            }
        };
        adapter.setOnItemClickListener(new OnItemClickListener<String>() {
            @Override
            public void onItemClick(ViewGroup parent, View view, String s, int position) {
                Toast.makeText(RecyclerViewActivity.this, "click" + position + ":" + s, Toast.LENGTH_SHORT).show();
            }

            @Override
            public boolean onItemLongClick(ViewGroup parent, View view, String s, int position) {
                Toast.makeText(RecyclerViewActivity.this, "longclick" + position + ":" + s, Toast.LENGTH_SHORT).show();
                return true;
            }
        });
        adapter.setHeader(R.layout.sample_header);
        adapter.setFooter(R.layout.sample_header);
        mRecyclerView.setAdapter(adapter);
//        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
//            @Override
//            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
//                super.onScrolled(recyclerView, dx, dy);
//                final LinearLayoutManager manager = (LinearLayoutManager)recyclerView.getLayoutManager();
//                if(mCurrentState == STATE_NORMAL && manager.findLastCompletelyVisibleItemPosition()==manager.getItemCount()-1){
//                    mCurrentState = STATE_LOAD;
//                    adapter.onLoadMoreStateChanged(true);
//                    mhandler.postDelayed(new Runnable() {
//                        @Override
//                        public void run() {
//                            mDatas.add("a");
//                            mDatas.add("b");
//                            adapter.notifyDataSetChanged();
//                            adapter.onLoadMoreStateChanged(false);
//                            mCurrentState = STATE_NORMAL;
//                        }
//                    },2000);
//                }
////                if (state == RecyclerView.SCROLL_STATE_IDLE){
////                    adapter.onLoadMoreStateChanged(true);
////                }
//            }
//
//            @Override
//            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
//                super.onScrollStateChanged(recyclerView, newState);
////                state = newState;
//            }
//        });
    }

    private void initDatas()
    {
        for (int i = 'A'; i < 'T'; i++)
        {
            mDatas.add((char) i + "");
        }
    }
}
